package com.xana.acg.mikomiko.actis.live.tiktok;

import android.opengl.Visibility;
import android.os.Build;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toolbar;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.xana.acg.com.app.Activity;
import com.xana.acg.com.app.ToolbarActivity;
import com.xana.acg.com.data.DataSource;
import com.xana.acg.com.view.recycler.RecyclerAdapter;
import com.xana.acg.com.view.recycler.RecyclerV;
import com.xana.acg.fac.model.biliLive.LiveItem;
import com.xana.acg.fac.model.biliLive.LivesResp;
import com.xana.acg.fac.model.biliLive.RecomItem;
import com.xana.acg.fac.model.biliLive.RecomResp;
import com.xana.acg.fac.model.biliLive.TabItem;
import com.xana.acg.fac.model.biliLive.TagItem;
import com.xana.acg.fac.net.NetCallBack;
import com.xana.acg.fac.net.Network;
import com.xana.acg.mikomiko.R;
import com.xana.acg.mikomiko.frags.live.FaceFragment;
import com.xana.acg.mikomiko.frags.live.LiveAreaFragment;
import com.xana.acg.mikomiko.listener.SelfDrawerListener;
import com.xana.acg.mikomiko.media.TiktokVideoView;

import java.util.List;

import butterknife.BindView;
import cn.jzvd.Jzvd;

public class TikTokActivity extends ToolbarActivity
        implements LiveAreaFragment.OnItemClick.Listener,
        TikTokRecyclerViewAdapter.OnClickListener,
        FaceFragment.OnClickListener, RecyclerV.OnMoreLoadListener {

    @BindView(R.id.rv_tiktok)
    RecyclerV rvTiktok;
    @BindView(R.id.drawer)
    DrawerLayout drawerLayout;

    @BindView(R.id.moreLiveDrawer)
    FrameLayout moreLive;

    private TikTokRecyclerViewAdapter mAdapter;
    private ViewPagerLayoutManager mViewPagerLayoutManager;
    private int mCurrentPosition = -1;

    @Override
    protected void initWindows() {
        super.initWindows();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tiktok;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        drawerLayout.addDrawerListener(new SelfDrawerListener());

        mAdapter = new TikTokRecyclerViewAdapter(this);
        mViewPagerLayoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);
        rvTiktok.setListener(this);
        rvTiktok.setLayoutManager(mViewPagerLayoutManager);
        rvTiktok.setAdapter(mAdapter);

        mViewPagerLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                //自动播放第一条
                autoPlayVideo(0);
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
                if (mCurrentPosition == position) {
                    Jzvd.releaseAllVideos();
                }
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                if (mCurrentPosition == position) {
                    return;
                }
                autoPlayVideo(position);
                mCurrentPosition = position;
            }
        });

        rvTiktok.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                Jzvd jzvd = view.findViewById(R.id.videoplayer);
                if (jzvd != null && Jzvd.CURRENT_JZVD != null && jzvd.jzDataSource != null &&
                        jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                    if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                        Jzvd.releaseAllVideos();
                    }
                }
            }
        });
    }


    private void autoPlayVideo(int postion) {
        if (rvTiktok == null || rvTiktok.getChildAt(0) == null) {
            return;
        }
        TiktokVideoView player = rvTiktok.getChildAt(0).findViewById(R.id.videoplayer);
        if (player != null) {
            player.startVideoAfterPreloading();
        }
    }

    int page;

    @Override
    protected void initData() {
        super.initData();
        request(++page);
    }

    private void request(int page) {
        if (tagItem == null)
            Network.biliAPI().getRecommend(page).enqueue(new NetCallBack<>(new DataSource.Callback<RecomResp>() {
                @Override
                public void ok(RecomResp data) {
                    for (RecomItem datum : data.getData()) {
                        mAdapter.add(datum);
                    }
                }

                @Override
                public void fail(String msg) {
                    showToast(msg);
                }
            }));

        else {
            Network.biliAPI().getLives(tagItem.getParent_id(), tagItem.getId(), page).enqueue(new NetCallBack<>(new DataSource.Callback<LivesResp>() {
                @Override
                public void ok(LivesResp resp) {
                    if (page == 1)
                        mAdapter.clear();
                    for (LiveItem liveItem : resp.data.list) {
                        mAdapter.add(liveItem);
                    }
                }

                @Override
                public void fail(String msg) {
                    showToast(msg);
                }
            }));
        }
        isFace = false;
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
            drawerLayout.closeDrawer(GravityCompat.END);
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }


    TagItem tagItem;

    @Override
    public void click(RecyclerAdapter.ViewHolder vh, TagItem item) {
        tagItem = item;
        drawerLayout.closeDrawer(GravityCompat.END);
        request(page = 1);
    }

    @Override
    public void click(View view) {
        switch (view.getId()) {
            case R.id.moreLiveTextView:
            case R.id.faceTextView:
                int vis = view.getId() == R.id.moreLiveTextView? View.VISIBLE: View.GONE;
                moreLive.getChildAt(0).setVisibility(vis);
                moreLive.getChildAt(1).setVisibility(View.GONE-vis);
                drawerLayout.openDrawer(GravityCompat.END);
                break;
        }
    }

    boolean isFace;
    @Override
    public void click(List list, int index) {
        drawerLayout.closeDrawer(GravityCompat.END);
        if(!isFace){
            mAdapter.replace(list);
            isFace = true;
        }
        rvTiktok.scrollToPosition(index);
    }



    @Override
    public void onMoreLoad() {
        if(isFace) return;
        request(++page);
    }
}
